package dale.web.link.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dale.web.common.model.*;
import dale.web.common.page.Pager;
import dale.web.link.model.Link;
import dale.web.link.repository.LinkRepository;

@Service
public class LinkServiceImpl implements LinkService {

	@Autowired
	private LinkRepository repository;
	
	@Override
	public TableData<Link> getData() {
		List<Link> list = repository.selectAll();
		return new TableData<Link>(list);
	}
	
	@Override
	public PagedList<Link> search(Condition<Link> condition) {
		int rowTotal = repository.count(condition);
		Pager pager = new Pager(condition, rowTotal);
		
		int offset = pager.getStartRow();
		int limit = pager.getListSize();
		List<Link> list = repository.selectList(condition, offset, limit);
		
		return new PagedList<Link>(pager, list);
	}
	
	@Override
	public Link find(Integer no) {
		return repository.selectOne(no);
	}
	
	@Override
	public void remove(Integer no) {
		if (repository.delete(no) != 1) {
			throw new RuntimeException("Nothing Removed");
		}
	}

	@Override
	public void create(Link item) {
		if (repository.insert(item) != 1) {
			throw new RuntimeException("Nothing Created");
		}
	}
	
	@Override
	public void modify(Link item) {
		if (repository.update(item) != 1) {
			throw new RuntimeException("Nothing Modified");
		}
	}

	@Override
	public void countUp(Integer no) {
		if (repository.countUp(no) != 1) {
			throw new RuntimeException("Can't count up.");
		}
	}

	@Override
	public List<String> categoryOptions() {
		return repository.selectDistinctCatecoryList();
	}
	
}
